/**
 * 检测满意度分析
 * @author yuan_rz
 * @date 2019/12/18 17:14
 */
define(['plugins/echarts/echarts.min'], function (echarts) {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        vueData = {
            displayTable: false,
            typeList: [],
            form: {
                TIME: 'MONTH'
            },
            timeList: [{
                value: 'MONTH', label: '本月'
            }, {
                value: 'LAST_MONTH', label: '上月'
            }, {
                value: 'QUARTER', label: '本季度'
            }, {
                value: 'YEAR', label: '本年'
            }]
        },
        moduleName = '检测满意度分析'
    ;

    app.newModule({
        init: function () {
            self = this;
            self._super({
                _tag: 'jcmyd'
            }, win);
        },
        /*initChart: function (type, list) {
            var legendList = [];
            var dataList = [];
            for (var i = 0; i < list.length; i++) {
                legendList.push(list[i].NAME);
                dataList.push({
                    name: list[i].NAME,
                    value: list[i].VALUE
                });
            }
            var divChart = echarts.init(doc.getElementById(type.ID));
            var option = {
                toolbox: {
                    feature: {
                        dataView: {show: true, readOnly: false},
                        saveAsImage: {show: true}
                    }
                },
                title: {
                    x: 'center',
                    y: 'top',
                    text: type.NAME
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    x: 'center',
                    y: 'bottom',
                    data: legendList
                },
                series: [
                    {
                        name: type.NAME,
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: true,
                                textStyle: {
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: dataList
                    }
                ]
            };
            divChart.setOption(option);
        },*/
        initChart: function (list) {
            var legendList = [];
            var dataList = [];
            for (var i = 0; i < list.length; i++) {
                legendList.push(list[i].NAME);
                dataList.push({
                    name: list[i].NAME,
                    value: list[i].VALUE
                });
            }
            var divChart = echarts.init(doc.getElementById('divChart'));
            var option = {
                toolbox: {
                    feature: {
                        dataView: {show: true, readOnly: false},
                        saveAsImage: {show: true}
                    }
                },
                title: {
                    x: 'center',
                    y: 'top',
                    text: '服务满意度'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c}颗星({d}%)"
                },
                legend: {
                    x: 'center',
                    y: 'bottom',
                    data: legendList
                },
                series: [
                    {
                        name: '服务满意度',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: true,
                                textStyle: {
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: dataList
                    }
                ]
            };
            divChart.setOption(option);
        },
        /*reloadTable: function () {
            for (var i = 0; i < vueData.typeList.length; i++) {
                (function (i) {
                    app.ajax({
                        xurl: 'bb/jcmyd/getListData',
                        xdata: {
                            TYPE: vueData.typeList[i].ID,
                            TIME: vueData.form.TIME
                        },
                        loading: true,
                        cb: function (result) {
                            if (result.ok) {
                                for (var j = 0; j < result.data.data.length; j++) {
                                    switch (result.data.data[j].NAME) {
                                        case '1':
                                            result.data.data[j].NAME = '一颗星';
                                            break;
                                        case '2':
                                            result.data.data[j].NAME = '两颗星';
                                            break;
                                        case '3':
                                            result.data.data[j].NAME = '三颗星';
                                            break;
                                        case '4':
                                            result.data.data[j].NAME = '四颗星';
                                            break;
                                        case '5':
                                            result.data.data[j].NAME = '五颗星';
                                            break;
                                        default:
                                            break;
                                    }
                                }
                                self.initChart(vueData.typeList[i], result.data.data);
                            }
                        }
                    });
                })(i);
            }
        },*/
        reloadTable: function () {
            app.ajax({
                xurl: 'bb/jcmyd/getListData',
                xdata: {
                    TIME: vueData.form.TIME
                },
                loading: true,
                cb: function (result) {
                    if (result.ok) {
                        self.initChart(result.data.data);
                    }
                }
            });
        },
        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    watch: {
                        'form.TIME': function (newValue, oldValue) {
                            self.reloadTable();
                        }
                    }
                }]
            });
        },
        _lazy: function () {
            this._super();
            /*app.ajax({
                xurl: 'bb/jcmyd/getTypeListData',
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        vueData.typeList = result.data.data;
                        self.reloadTable();
                    }
                }
            });*/
            self.reloadTable();
        }
    });
});